package com.xunce.xcworkservice.controller;

import com.xunce.xcworkservice.dto.RolePermissionDto;
import com.xunce.xcworkservice.entity.AccountVerify;
import com.xunce.xcworkservice.entity.Department;
import com.xunce.xcworkservice.entity.Menus;
import com.xunce.xcworkservice.entity.Roles;
import com.xunce.xcworkservice.exception.CustomException;
import com.xunce.xcworkservice.response.R;
import com.xunce.xcworkservice.service.DepartmentService;
import com.xunce.xcworkservice.service.MenusService;
import com.xunce.xcworkservice.service.RolesService;
import com.xunce.xcworkservice.vo.IPageVo;
import com.xunce.xcworkservice.vo.UserInfoVo;
import com.xunce.xcworkservice.vo.UserSimpleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yanlong.guo
 * 系统管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys")
@Tag(name = "系统管理")
public class SysController extends BaseController {

    private final DepartmentService departmentService;

    private final RolesService rolesService;

    private final MenusService menusService;

    @Operation(summary = "部门管理-列表", description = "部门列表")
    @GetMapping("/getDepartmentList")
    public R<List<Department>> getDepartmentList(){
        List<Department> departments = departmentService.getTreeList();
        return  R.success(departments);
    }

    @Operation(summary = "部门管理-编辑/新增", description = "部门编辑/新增")
    @PostMapping("/departmentEdit")
    public R<String> departmentEdit(@RequestBody @Validated Department department){
        boolean res = departmentService.edit(department);
        return  res ? R.success() : R.error();
    }

    @Operation(summary = "部门管理-删除", description = "部门管理-删除")
    @GetMapping("/departmentDelete")
    public R<String> departmentDelete(@RequestParam @Schema(description = "部门id") @NotNull(message = "部门id不能为空") Integer departmentId) throws CustomException {
        boolean res = departmentService.checkDelete(departmentId);
        return  res ? R.success() : R.error();
    }

    @Operation(summary = "审核管理-列表", description = "部门编辑/新增")
    @GetMapping("/getVerifyList")
    public R<List<AccountVerify>> getVerifyList(){
        List<AccountVerify> accountVerifies = departmentService.getVerifyList();
        return R.success(accountVerifies);
    }


    @Operation(summary = "审核管理-添加/编辑", description = "审核管理-添加/编辑")
    @PostMapping("/verifyEdit")
    public R<String> verifyEdit(@RequestBody @Validated AccountVerify accountVerify){
        boolean res = departmentService.verifyEdit(accountVerify);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "审核管理-删除", description = "审核管理-删除")
    @GetMapping ("/verifyDelete")
    public R<String> verifyDelete(@RequestParam @NotNull(message = "不能为空") Integer id){
        boolean res = departmentService.verifyDelete(id);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "角色管理-列表", description = "角色管理-列表")
    @GetMapping("/getRoleList")
    public R<List<Roles>> getRoleList(@RequestParam(required = false) @Schema(description = "是否需要权限") boolean permission){
        List<Roles> rolesList = rolesService.getList(permission);
        return R.success(rolesList);
    }

    @Operation(summary = "角色管理-编辑/新增", description = "角色编辑/新增, 新增不需要id")
    @PostMapping("/roleEdit")
    public R<String> roleEdit(@RequestBody @Validated Roles roles){
        boolean res = rolesService.edit(roles);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "角色管理-禁用/启用", description = "角色编辑/新增, 新增不需要id")
    @GetMapping("/setRoleState")
    public R<String> setRoleState(@RequestBody @NotNull Integer id,
                                  @RequestBody @NotNull Integer state){
        boolean res = rolesService.setSate(id, state);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "角色管理-删除", description = "角色删除, 使用中的角色无法删除")
    @GetMapping("/roleDelete")
    public R<String> roleDelete(@RequestParam @Schema(description = "角色id不能为空") @NotNull Integer id){
        boolean res = rolesService.checkDelete(id);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "角色管理-权限设置", description = "角色删除, 使用中的角色无法删除")
    @PostMapping("/rolePermissionEdit")
    public R<String> rolePermissionEdit(@RequestBody RolePermissionDto rolePermissionDto){
        boolean res = rolesService.rolePermissionEdit(rolePermissionDto.getRoleId(), rolePermissionDto.getPermission());
        return res ? R.success() : R.error();
    }

    @Operation(summary = "菜单管理-列表", description = "菜单管理-列表")
    @GetMapping("/getMenuList")
    public R<List<Menus>> getMenuList(){
        List<Menus> menusList = menusService.getList();
        return R.success(menusList);
    }

    @Operation(summary = "菜单管理-编辑/新增", description = "菜单管理/新增, 新增不需要id")
    @PostMapping("/menuEdit")
    public R<String> menuEdit(@RequestBody @Validated Menus menus){
        boolean res = menusService.doEdit(menus);
        return res ? R.success() : R.error();
    }

    @Operation(summary = "菜单管理-删除", description = "菜单管理-删除")
    @GetMapping("/menuDelete")
    public R<String> menuDelete(@RequestParam @Schema(description = "菜单id不能为空") @NotNull Integer id){
        boolean res = menusService.checkDelete(id);
        return res ? R.success() : R.error();
    }
}
